package cn.tedu.seckill.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SenderController {
    @Autowired
    RabbitTemplate template;

    @RequestMapping("send")
    public String sendMsg(String msg) {
        // 直接将消息msg作为消息体的内容，让template发送
        /**
         * exchange: 交换机名称
         * routingKey: 路由Key
         * msg: Object类型msg，自动进行byte转化
         */
//        template.convertAndSend("directEx", "北京", msg);

        // 相当于 channel.basicPublish("ex", "routing", msg.getBytes());
        // 关心发送消息时，消息有一些属性要携带，使用send方法
//        MessageProperties properties = new MessageProperties();
//        properties.setPriority(100);
//        properties.setUserId("guest");
//        Message message = new Message(msg.getBytes(), properties);
//        template.send("directExchange", "beijing", message);
//        return "success";

        template.convertAndSend("seckillD01", "seckill", msg);
        return "success";
    }
}
